package com.example.neusoft.a16110100126yttordering.activities;

import android.content.Intent;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.example.neusoft.a16110100126yttordering.R;

import cn.bmob.sms.BmobSMS;
import cn.bmob.sms.exception.BmobException;
import cn.bmob.sms.listener.RequestSMSCodeListener;
import cn.bmob.sms.listener.VerifySMSCodeListener;

public class PhoneRegisterActivity extends BaseActivity {
    private EditText et_phone,et_password,et_checkNum,editText11;
    private ImageButton imageButton;
    private Button btn_send,btn_reg;
    private TextView textView3;
    private boolean result=false;

    @Override
    void initViews() {
        setContentView(R.layout.activity_phone_register);
        et_phone=findViewById(R.id.editText8);
        et_password=findViewById(R.id.editText9);
        et_checkNum=findViewById(R.id.editText10);
        editText11=findViewById(R.id.editText11);
        imageButton=findViewById(R.id.imageButton1);
        btn_send=findViewById(R.id.button6);
        btn_reg=findViewById(R.id.button7);
        textView3=findViewById(R.id.textView4);
    }

    @Override
    void initEvents() {
        btn_reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Intent intent=new Intent();
//                intent.setClass(PhoneRegisterActivity.this,LoginActivity.class);
//                startActivity(intent);
                if(checkPhone())
                    register();
            }
        });
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent();
                intent.setClass(PhoneRegisterActivity.this,LoginActivity.class);
                startActivity(intent);
            }
        });
        btn_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //接受验证码到手机
                BmobSMS.requestSMSCode(PhoneRegisterActivity.this, et_phone.getText().toString(),
                        "短信模板",
                        new RequestSMSCodeListener() {
                    @Override
                    public void done(Integer integer, BmobException e) {
                        if(e==null){
                            new CountDownTimer(60000,1000){
                                @Override
                                public void onTick(long millisUntilFinished) {
                                    btn_send.setText(millisUntilFinished/1000+"秒");
                                }
                                @Override
                                public void onFinish() {
                                    btn_send.setClickable(true);
                                    btn_send.setText("重新发送");
                                }
                            }.start();
                            showToast("验证码发送成功，请尽快验证");
                        }else {
                            Log.e("btn_send",e.toString());
                            showToast("验证码发送失败");
                        }
                    }
                });
            }
        });
    }

    @Override
    void initData() {
    BmobSMS.initialize(this,"20d4d74ff9f6c5834d35db27e55f0f86");
    }
    boolean checkPhone(){
        BmobSMS.verifySmsCode(PhoneRegisterActivity.this, et_phone.getText().toString(), et_checkNum.getText().toString(), new VerifySMSCodeListener() {
            @Override
            public void done(BmobException e) {
                if(e==null){
                    showToast("验证成功");
                    result=true;
                }else {
                    Log.e("btn_check",e.toString());
                    showToast("验证失败");
                }
            }
        });
        return result;
    }
    public void register(){

    }

}
